package com.zeropol2.anyquestion.component.ui.list;

import java.util.List;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;

import com.zeropol2.anyquestion.R;
import com.zeropol2.anyquestion.component.ui.widget.ImageTextButton;

/**
 * @author zeropol2
 * 이미지와 텍스트가있는 버튼들의 탭 컴포넌트
 */
public class ImageTextButtonTab extends LinearLayout {
	private Context mContext;
	private LayoutInflater mInflater;
	private LinearLayout mLayout;
	private LinearLayout mMyLayout;
	private ImageTextButton mImageTextButton;
	private List<ImageTextButton> mList;
	private ItemSelectedListener mItemSelectedListener;

	public static interface ItemSelectedListener {
		public void onItemSelected(int position);
	}

	// 생성자
	public ImageTextButtonTab(Context context) {
		super(context);
		mContext = context;
		init();
	}

	public ImageTextButtonTab(Context context, AttributeSet attrs) {
		super(context, attrs);
		mContext = context;
		init();
	}

	// 초기화
	private void init() {
		mInflater = (LayoutInflater) mContext
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		mLayout = (LinearLayout) mInflater.inflate(
				R.layout.image_text_button_tab, this);
		mMyLayout = (LinearLayout) mLayout
				.findViewById(R.id.image_text_button_tab_layout);
	}
	
	
	/**
	 * position의 탭 아이템 반환
	 * @param position
	 * @return
	 */
	public ImageTextButton getTabItem(int position) {
		return mList.get(position);
	}

	/**
	 * 탭을 선택하는 메소드
	 * @param position
	 */
	public void setTabLocation(int position) {
		for(int i=0;i<mList.size();i++) {
			mImageTextButton = mList.get(i);
			if(i != position) {
				mImageTextButton.setSelected(false);
			} else {
				mImageTextButton.setSelected(true);
			}
		}
	}

	/**
	 * 탭 컴포넌트에 아이템 꽂아주는 메소드
	 * @param list
	 */
	public void setTabItem(List<ImageTextButton> list) {
		LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT,1.0f);
		
		mList = list;
		for(int i=0;i<mList.size();i++) {
			mImageTextButton = mList.get(i);
			mImageTextButton.setId(i);
			mMyLayout.addView(mImageTextButton,layoutParams);
			
			mImageTextButton.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					mItemSelectedListener.onItemSelected(v.getId());
				}
			});
		}
	}

	
	/**
	 * 리스너 설정
	 * @param l
	 */
	public void setItemSelectedListener(ItemSelectedListener l) {
		mItemSelectedListener = l;
	}
}
